package com.edu.kindergarten.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.edu.kindergarten.R;
import com.edu.kindergarten.entity.BabyTreeEntity;
import com.edu.kindergarten.entity.CommentEntity;
import com.edu.kindergarten.utils.LogUtils;
import com.edu.kindergarten.utils.UiUtils;


public class BabyTreeListAdapter extends BaseAdapter {

	private static final String TAG = "BabyTreeListAdapter";

	private Context context;
	private int layoutId;
	private List<BabyTreeEntity> listData;
	private int to[];
	
	public BabyTreeListAdapter(Context context, int layoutId,
			List<BabyTreeEntity> listData, int to[]) {
		this.context = context;
		this.layoutId = layoutId;
		this.listData = listData;
		this.to = to;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		LogUtils.d(TAG, "constructor,listData size:" + listData.size());
		return listData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		LogUtils.d(TAG, "getView,position=" + position);

		if (convertView == null) {
			LayoutInflater layoutInflator = LayoutInflater.from(context);
			convertView = layoutInflator.inflate(layoutId,parent,false);
			holder = new ViewHolder();
			holder.timeView = (TextView) convertView.findViewById(to[0]);
			holder.contentView = (TextView) convertView.findViewById(to[1]);
			holder.gridView = (GridView) convertView.findViewById(to[2]);
			holder.commentView = (LinearLayout) convertView.findViewById(to[3]);
			holder.agreeView = (TextView) convertView.findViewById(to[4]);
			holder.addCommentView = (TextView) convertView.findViewById(to[5]);
			holder.agreeView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					UiUtils.showShortToast(context, R.string.function_not_implement);
				}
			});
			holder.addCommentView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					UiUtils.showShortToast(context, R.string.function_not_implement);
				}
			});
			
			holder.commentCount = 0;
			
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.timeView.setText(listData.get(position).getTime());
		holder.contentView.setText(listData.get(position).getContent());
		
		if(holder.gridView.getAdapter() == null){
			ArrayList<String> imageList = listData.get(position).getImageList();
			if (imageList != null) {
				holder.gridView.setAdapter(new SingleImageAdapter(context, 
						R.layout.layout_item_single_image, 
						imageList, 
						new int[]{R.id.iv_main}));
			}
		}
		
		ArrayList<CommentEntity> commentList = listData.get(position).getCommentList();
		if (commentList != null && commentList.size() > 0 && commentList.size() != holder.commentCount) {
			//holder.commentView.removeAllViews();
			LayoutInflater layoutInflator = LayoutInflater.from(context);
			int len = commentList.size();
			holder.commentCount = len;
			for (int i = 0; i < len; i++) {
				CommentEntity entity = commentList.get(i);
				View unit = layoutInflator.inflate(R.layout.layout_item_comment, parent, false);
				TextView nameView = (TextView)unit.findViewById(R.id.comment_name);
				TextView contentView = (TextView)unit.findViewById(R.id.comment_content);
				nameView.setText(entity.getName());
				contentView.setText(entity.getContent());
				holder.commentView.addView(unit);
			}
		}
		//holder.imageView.setImageResource(listData.get(position).image);
		//holder.textView.setText(listData.get(position).txt);
		//ImageLoader.getInstance().displayImage("drawable://" + listData[position], holder.imageView);

		return convertView;
	}

	static class ViewHolder {
		TextView timeView;
		TextView contentView;
		GridView gridView;
		LinearLayout commentView;
		
		TextView agreeView;
		TextView addCommentView;
		int commentCount;
		
	}

}
